package com.tbit.uqbike.service.business.impl;


import cn.hutool.core.lang.TypeReference;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.ZfbPayConfig;
import com.tbit.uqbike.service.business.ZfbPayConfigService;
import com.tbit.uqbike.webmanager.dao.core.ZfbPayConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("zfbPayConfigService")
public class ZfbPayConfigServiceImpl implements ZfbPayConfigService {
    @Autowired
    private ZfbPayConfigDao zfbPayConfigDao;
    @Resource
    private WeixinPayConfigServiceImpl weixinPayConfigService;

    @Override
    public ZfbPayConfig getByAccountId(Integer accountId, Integer type) {
        return zfbPayConfigDao.getByAccountId(accountId, type);
    }

    @Override
    public void add(ZfbPayConfig zfbPayConfig) {
        //记录商户变更日志
        Map<String, Object> requestBody = JSON.parseObject(JSON.toJSONString(zfbPayConfig), new TypeReference<Map<String, Object>>() {
        });
        weixinPayConfigService.addLog(zfbPayConfig.getAccountId(), 1, zfbPayConfig.getOpId(), requestBody);

        ZfbPayConfig zfbPayConfigDB = getByAccountId(zfbPayConfig.getAccountId(), zfbPayConfig.getType());
        if (zfbPayConfigDB != null) {
            if (zfbPayConfig.getRemark() == null) {
                zfbPayConfig.setRemark(zfbPayConfigDB.getRemark());
            }
            zfbPayConfigDao.edit(zfbPayConfig);
        } else {
            zfbPayConfigDao.add(zfbPayConfig);
        }
    }


}